package ua.com.alus.resvac.crm.stripes.system.locale;

import net.sourceforge.stripes.config.Configuration;
import net.sourceforge.stripes.localization.DefaultLocalizationBundleFactory;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * Created by Usatov Alexey
 * <p/>
 * We just replace getting ResourceBundle - we get all resource bundles with UTF-8 encoding.
 */
public class CurrentLocalizationBundleFactory extends DefaultLocalizationBundleFactory {

    private String errorBundleName;
    private String fieldBundleName;

    public void init(Configuration configuration) throws Exception {
        setConfiguration(configuration);

        this.errorBundleName = configuration.getBootstrapPropertyResolver().
                getProperty(ERROR_MESSAGE_BUNDLE);
        if (this.errorBundleName == null) {
            this.errorBundleName = BUNDLE_NAME;
        }

        this.fieldBundleName = configuration.getBootstrapPropertyResolver().
                getProperty(FIELD_NAME_BUNDLE);
        if (this.fieldBundleName == null) {
            this.fieldBundleName = BUNDLE_NAME;
        }
    }

    @Override
    public ResourceBundle getErrorMessageBundle(Locale locale) throws MissingResourceException {
        try {
            if (locale == null) {
                return ResourceBundle.getBundle(this.errorBundleName, new UTF8Control());
            } else {
                return ResourceBundle.getBundle(this.errorBundleName, locale, new UTF8Control());
            }
        } catch (MissingResourceException mre) {
            MissingResourceException mre2 = new MissingResourceException(
                    "Could not find the error message resource bundle needed by Stripes. This " +
                            "almost certainly means that a properties file called '" +
                            this.errorBundleName + ".properties' could not be found in the classpath. " +
                            "This properties file is needed to lookup validation error messages. Please " +
                            "ensure the file exists in WEB-INF/classes or elsewhere in your classpath.",
                    this.errorBundleName, "");
            mre2.setStackTrace(mre.getStackTrace());
            throw mre2;
        }
    }

    @Override
    public ResourceBundle getFormFieldBundle(Locale locale) throws MissingResourceException {
        try {
            if (locale == null) {
                return ResourceBundle.getBundle(this.fieldBundleName, new UTF8Control());
            } else {
                return ResourceBundle.getBundle(this.fieldBundleName, locale, new UTF8Control());
            }
        } catch (MissingResourceException mre) {
            MissingResourceException mre2 = new MissingResourceException(
                    "Could not find the form field resource bundle needed by Stripes. This " +
                            "almost certainly means that a properties file called '" +
                            this.fieldBundleName + ".properties' could not be found in the classpath. " +
                            "This properties file is needed to lookup form field names. Please " +
                            "ensure the file exists in WEB-INF/classes or elsewhere in your classpath.",
                    this.fieldBundleName, "");
            mre2.setStackTrace(mre.getStackTrace());
            throw mre2;
        }
    }
}
